/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.product;

import java.util.List;

import org.openaecfm.model.actor.PostalAddress;
import org.openaecfm.model.product.Site;
import org.openaecfm.model.product.SpatialStructureElement;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class SiteIO extends AbstractEntityInstanceInputImpl<Site>
		implements IEntityInstanceOutput<Site> {
	private IEntityInstanceUtilInput<SpatialStructureElement> utilInput;
	private IEntityInstanceUtilOutput<SpatialStructureElement> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SpatialStructureElement> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Site pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCSITE",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set RefLatitude
		if (pojo.getRefLatitude().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Integer i : pojo.getRefLatitude()) {
				l.add(new IntegerParameter(i));
			}
			ei.addParameter(l);
		}

		// Set RefLongitude
		if (pojo.getRefLongitude().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Integer i : pojo.getRefLongitude()) {
				l.add(new IntegerParameter(i));
			}
			ei.addParameter(l);
		}

		// Set RefElevation
		if (pojo.getRefElevation() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getRefElevation()));
		}

		// Set LandTitleNumber
		if (pojo.getLandTitleNumber() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLandTitleNumber(),
					StringParameter.UTF_8));
		}

		// Set SiteAddress
		if (pojo.getSiteAddress() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getSiteAddress())));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Site doCreateObject() {
		return new Site();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Site pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SiteAddress
		Parameter siteadd = sei.getParameter(13);
		if (siteadd.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) siteadd;
			pojo.setSiteAddress((PostalAddress) appDataManager.getObject(n
					.toInstanceName()));
		}
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<SpatialStructureElement> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Site pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RefLatitude
		Parameter refla = sei.getParameter(9);
		if (refla.getNodeType() == NodeType.LIST) {
			List<Integer> refLatitude = new java.util.ArrayList<Integer>();
			ListParameter l = (ListParameter) refla;
			for (Parameter p : l) {
				IntegerParameter i = (IntegerParameter) p;
				refLatitude.add(i.toInteger());
			}
			pojo.setRefLatitude(refLatitude);
		}

		// Set RefLongitude
		Parameter reflo = sei.getParameter(10);
		if (reflo.getNodeType() == NodeType.LIST) {
			List<Integer> refLongitude = new java.util.ArrayList<Integer>();
			ListParameter l = (ListParameter) reflo;
			for (Parameter p : l) {
				IntegerParameter i = (IntegerParameter) p;
				refLongitude.add(i.toInteger());
			}
			pojo.setRefLongitude(refLongitude);
		}

		// Set RefElevation
		Parameter refel = sei.getParameter(11);
		if (refel.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) refel;
			pojo.setRefElevation(r.toDouble());
		}

		// Set LandTitleNumber
		Parameter langtitle = sei.getParameter(12);
		if (langtitle.getNodeType() == NodeType.STRING) {
			StringParameter sp = (StringParameter) langtitle;
			pojo.setLandTitleNumber(sp.toUTF8());
		}
	}
}
